package com.yyx.app1.Data.Medication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yyx.app1.Data.DataInit;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yuexing on 2015/9/25.
 */
public class MedicationFirstList_SQLiteOperation {
    private DataInit dataInit;
    private String TableName;

    public MedicationFirstList_SQLiteOperation(DataInit dataInit) {
        this.dataInit=dataInit;
        this.TableName=dataInit.getDB_MedicationFirstListTableName();
    }

    public void add_safe(Firstcolumns firstcolumns)//已经有了就不添加
    {
        if (!find(firstcolumns.getFcname()))
        {
            add(firstcolumns);
        }
    }

    public void deleteAll()
    {
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            db.execSQL("delete from "+TableName+" ;");
        }
        catch (Exception e)
        {
            Log.e("异常1509301855",e.toString());
        }
        finally
        {
            Log.i("medicationfirstlist","deleteAll");
            db.close();
        }
    }

    public long add(Firstcolumns firstcolumns) {
        long result=0;
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            ContentValues values = new ContentValues();
            values.put("id",firstcolumns.getId());//private String id;
            values.put("fcname",firstcolumns.getFcname());//private String fcname;         //栏目名称
            values.put("seoTitle",firstcolumns.getSeoTitle());//private String seoTitle;       //SEO标题
            values.put("fcdescription",firstcolumns.getFcdescription());//private String fcdescription;  //SEO描述
            values.put("fckeyWord",firstcolumns.getFckeyWord());//private String fckeyWord;      //SEO关键词
            values.put("modulesId",firstcolumns.getModulesId());//private Integer modulesId;     //栏目所属模块
            values.put("addTime",firstcolumns.getAddTime());//private String addTime;        //创建时间
            values.put("orderby",firstcolumns.getOrderby());//private Integer orderby;       //排序顺序
            values.put("url",firstcolumns.getUrl());//private String url;            //路径
            values.put("urlrewrite", firstcolumns.getUrlrewrite());//private String urlrewrite;     //真实路径
            result = db.insert(TableName,null,values);
        }
        catch (Exception e)
        {
            Log.e("异常1511091713",e.toString());
        }
        finally
        {
            db.close();
            return result;
        }
    }

    public long add(Firstcolumns firstcolumns,SQLiteDatabase db) {
        long result=0;
        try
        {
            ContentValues values = new ContentValues();
            values.put("id",firstcolumns.getId());//private String id;
            values.put("fcname",firstcolumns.getFcname());//private String fcname;         //栏目名称
            values.put("seoTitle",firstcolumns.getSeoTitle());//private String seoTitle;       //SEO标题
            values.put("fcdescription",firstcolumns.getFcdescription());//private String fcdescription;  //SEO描述
            values.put("fckeyWord",firstcolumns.getFckeyWord());//private String fckeyWord;      //SEO关键词
            values.put("modulesId",firstcolumns.getModulesId());//private Integer modulesId;     //栏目所属模块
            values.put("addTime",firstcolumns.getAddTime());//private String addTime;        //创建时间
            values.put("orderby",firstcolumns.getOrderby());//private Integer orderby;       //排序顺序
            values.put("url",firstcolumns.getUrl());//private String url;            //路径
            values.put("urlrewrite", firstcolumns.getUrlrewrite());//private String urlrewrite;     //真实路径
            result = db.insert(TableName,null,values);
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            return result;
        }
    }

    public boolean find(String fcname) {
        boolean result=false;
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            Cursor cursor = db.query(TableName, null, "fcname=?", new String[]{fcname}, null, null, null);
            result = cursor.moveToNext();
            cursor.close();
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            return result;
        }
    }

    public int delete(String fcname) {
        int count=0;
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            count = db.delete(TableName, "fcname=?", new String[]{fcname});
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            return count;
        }
    }

    public JSONObject getBy_Firstcolumns(String fcname) {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        JSONObject jsonObject=new JSONObject();
        try
        {
            db =dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, null, "fcname=?", new String[]{fcname}, null, null, null, null);
            if (cursor.moveToNext())
            {
                jsonObject.put("id",cursor.getString(cursor.getColumnIndex("id")));    //private String id;
                jsonObject.put("fcname",cursor.getString(cursor.getColumnIndex("fcname")));    //private String fcname;         //栏目名称
                jsonObject.put("seoTitle",cursor.getString(cursor.getColumnIndex("seoTitle")));    //private String seoTitle;       //SEO标题
                jsonObject.put("fcdescription",cursor.getString(cursor.getColumnIndex("fcdescription")));    //private String fcdescription;  //SEO描述
                jsonObject.put("fckeyWord",cursor.getString(cursor.getColumnIndex("fckeyWord")));    //private String fckeyWord;      //SEO关键词
                jsonObject.put("modulesId",cursor.getInt(cursor.getColumnIndex("modulesId")));    //private Integer modulesId;     //栏目所属模块
                jsonObject.put("addTime",cursor.getString(cursor.getColumnIndex("addTime")));    //private String addTime;        //创建时间
                jsonObject.put("orderby",cursor.getInt(cursor.getColumnIndex("orderby")));    //private Integer orderby;       //排序顺序
                jsonObject.put("url",cursor.getString(cursor.getColumnIndex("url")));    //private String url;            //路径
                jsonObject.put("urlrewrite",cursor.getString(cursor.getColumnIndex("urlrewrite")));    //private String urlrewrite;     //真实路径
            }
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            cursor.close();
            return jsonObject;
        }
    }

    public List getAll() {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<HashMap<String,String>> list=new ArrayList<>();
        try
        {
            db =dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"id","fcname"}, null, null,null,null,null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put("id",cursor.getString(cursor.getColumnIndex("id")));
                hashMap.put("fcname",cursor.getString(cursor.getColumnIndex("fcname")));
                list.add(hashMap);
            }
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public void InjectionSQList()
    {
        SQLiteDatabase db = dataInit.creatOrOpenDatabase();
        File file=new File(dataInit.getDataBaseDirectory()+"/"+dataInit.getMedicationFirstListDataFileName());
        try
        {
            BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
            String strline = null;
            while ((strline=bufferedReader.readLine())!=null)
            {
                JSONObject jsonObject=new JSONObject(strline);
                Firstcolumns firstcolumns = new Firstcolumns();
                firstcolumns.setId(jsonObject.getString("id"));//private String id;
                firstcolumns.setFcname(jsonObject.getString("fcname"));//private String fcname;         //栏目名称
                firstcolumns.setSeoTitle(jsonObject.getString("seoTitle"));//private String seoTitle;       //SEO标题
                firstcolumns.setFcdescription(jsonObject.getString("fcdescription"));//private String fcdescription;  //SEO描述
                firstcolumns.setFckeyWord(jsonObject.getString("fckeyWord"));//private String fckeyWord;      //SEO关键词
                firstcolumns.setModulesId(jsonObject.getInt("modulesId"));//private Integer modulesId;     //栏目所属模块
                firstcolumns.setAddTime(jsonObject.getString("addTime"));//private String addTime;        //创建时间
                firstcolumns.setOrderby(jsonObject.getInt("orderby"));//private Integer orderby;       //排序顺序
                firstcolumns.setUrl(jsonObject.getString("url"));//private String url;            //路径
                firstcolumns.setUrlrewrite(jsonObject.getString("urlrewrite"));//private String urlrewrite;     //真实路径
                add(firstcolumns,db);
            }
            bufferedReader.close();
        }
        catch (Exception e)
        {
            Log.e("异常", e.toString());
        }
        finally
        {
            Log.i("firstcolumns", "结束");
            db.close();
        }
    }
}
